//
// Created by jinbang.li on 2023/5/21.
//

#include "IOpDecode.h"
#include <thread>
using namespace std;
void sleep2(int millseconds){
    chrono::milliseconds  du(millseconds);
    this_thread::sleep_for(du);
}
void IOpDecode::update(OpData opData) {
    if (opData.isAudio != isAudio){
        return;
    }
    while (!isExit){
        packetMutex.lock();
        if (packets.size() < maxList){
            packets.push_back(opData);
            packetMutex.unlock();
            break;
        }
        packetMutex.unlock();
        sleep2(1);
    }
}
void IOpDecode::Main() {
    while (!isExit){
        packetMutex.lock();
        if (packets.empty()){
            packetMutex.unlock();
            sleep2(1);
            continue;
        }
        OpData packet = packets.front();
        packets.pop_front();
        //发送数据到解码线程，一个数据包，可能解码多个结果
        if (this->SendPacket(packet)){
            while (!isExit){
                OpData frame = RecvFrame();
                if (!frame.data)break;
                this->notify(frame);
            }
        }
        packet.dropData();
        packetMutex.unlock();
    }
}